package com.baihan.angus.app.game.busshow.adps;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.utils.ComUtil;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.pop_dialog.SeatInfoPopup;
import com.baihan.angus.app.game.common.bean.response.ScriptRole;
import com.baihan.angus.app.game.common.configs.UserConfig;
import com.baihan.angus.app.game.common.util.ImageLoader;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.util.List;

public class VoiceRoomSeatAdp extends RecyclerView.Adapter {

    private Context context;
    private OnItemClickListener mOnItemClickListener;
    private OnPopupItemClickListener mOnPopupItemClickListener;
    private int selectType = 0;
    private SeatInfoPopup mSeatInfoPopup;
    private List<ScriptRole> list;
    private String userId;

    public static final int VIEWTYPE_LEFT = 1;//左侧;
    public static final int VIEWTYPE_RIGHT = 2;//右侧;

    public VoiceRoomSeatAdp(Context context, List<ScriptRole> list, String userId,
                            OnItemClickListener onItemClickListener,
                            OnPopupItemClickListener onPopupItemClickListener) {
        this.context = context;
        this.list = list;
        this.userId = userId;
        this.mOnItemClickListener = onItemClickListener;
        this.mOnPopupItemClickListener = onPopupItemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        View view;
        switch (viewType) {
            case VIEWTYPE_LEFT:
                if (!UserConfig.getUserConfig().getUser().getUser_id().equals(userId)) {
                    view = inflater.inflate(R.layout.item_voice_room_audience_seat_left, parent, false);
                    return new AudienceViewHolder(view);
                } else {
                    view = inflater.inflate(R.layout.item_voice_room_dm_seat_left, parent, false);
                    return new DmViewHolder(view);
                }
            case VIEWTYPE_RIGHT:
                if (!UserConfig.getUserConfig().getUser().getUser_id().equals(userId)) {
                    view = inflater.inflate(R.layout.item_voice_room_audience_seat_right, parent, false);
                    return new AudienceViewHolder(view);
                } else {
                    view = inflater.inflate(R.layout.item_voice_room_dm_seat_right, parent, false);
                    return new DmViewHolder(view);
                }
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof AudienceViewHolder) {
            ScriptRole item = list.get(position);
            AudienceViewHolder vh = (AudienceViewHolder) holder;
            vh.bind(context, item, mOnItemClickListener);

            vh.ivAudienceAvatar.setOnClickListener(view -> {
                showPopup(vh, position);
            });
        } else if (holder instanceof DmViewHolder) {
            ScriptRole item = list.get(position);
            DmViewHolder vh = (DmViewHolder) holder;
            vh.bind(context, item, mOnItemClickListener);

            vh.ivAudienceAvatar.setOnClickListener(view -> {
                showPopup(vh, position);
            });
        }
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return VIEWTYPE_LEFT;
        } else if (position % 2 == 1) {
            return VIEWTYPE_RIGHT;
        }
        return 0;
    }

    private void showPopup(@NonNull RecyclerView.ViewHolder holder, int position) {
            mSeatInfoPopup = new SeatInfoPopup(context, selectType, new SeatInfoPopup.OnSelectListener() {
                @Override
                public void onSelect(int type) {
                    selectType = type;
                    if (type == 1) {
                        mOnPopupItemClickListener.onUserPoster(position);
                    } else if (type == 2) {
                        mOnPopupItemClickListener.onScriptPoster(position);
                    } else if (type == 3) {
                        mOnPopupItemClickListener.onBgStory(position);
                    }
                    mSeatInfoPopup.dismiss();
                }
            });
        int viewType = getItemViewType(position);
        int[] location = new int[2];
        if (holder instanceof AudienceViewHolder) {
            AudienceViewHolder vh = (AudienceViewHolder) holder;
            vh.ivAudienceAvatar.getLocationOnScreen(location);

            if (!mSeatInfoPopup.isShowing()) {
                switch (viewType) {
                    case VIEWTYPE_LEFT:
                        mSeatInfoPopup.showAtLocation(vh.ivAudienceAvatar,
                                Gravity.NO_GRAVITY,
                                location[0] + vh.ivAudienceAvatar.getMeasuredWidth() + ComUtil.Dp2Px(context, 10),
                                location[1]);
                        break;
                    case VIEWTYPE_RIGHT:
                        mSeatInfoPopup.showAtLocation(vh.ivAudienceAvatar,
                                Gravity.NO_GRAVITY,
                                location[0] - vh.ivAudienceAvatar.getMeasuredWidth() - ComUtil.Dp2Px(context, 10),
                                location[1]);
                        break;
                }
            }
        } else if (holder instanceof DmViewHolder) {
            DmViewHolder vh = (DmViewHolder) holder;
            vh.ivAudienceAvatar.getLocationOnScreen(location);

            if (!mSeatInfoPopup.isShowing()) {
                switch (viewType) {
                    case VIEWTYPE_LEFT:
                        mSeatInfoPopup.showAtLocation(vh.ivAudienceAvatar,
                                Gravity.NO_GRAVITY,
                                location[0] + vh.ivAudienceAvatar.getMeasuredWidth() + ComUtil.Dp2Px(context, 10),
                                location[1]);
                        break;
                    case VIEWTYPE_RIGHT:
                        mSeatInfoPopup.showAtLocation(vh.ivAudienceAvatar,
                                Gravity.NO_GRAVITY,
                                location[0] - vh.ivAudienceAvatar.getMeasuredWidth() - ComUtil.Dp2Px(context, 10),
                                location[1]);
                        break;
                }
            }
        }
    }

    public static class AudienceViewHolder extends RecyclerView.ViewHolder {

        ImageView ivJoin, ivVoice, ivBorder;
        QMUIRadiusImageView ivAudienceAvatar;
        TextView tvAudienceName, tvAlready;
        LinearLayout llSeat, llStatus;

        public AudienceViewHolder(@NonNull View itemView) {
            super(itemView);
            ivJoin = itemView.findViewById(R.id.ivJoin);
            ivAudienceAvatar = itemView.findViewById(R.id.ivAudienceAvatar);
            tvAudienceName = itemView.findViewById(R.id.tvAudienceName);
            ivVoice = itemView.findViewById(R.id.ivVoice);
            ivBorder = itemView.findViewById(R.id.ivBorder);
            tvAlready = itemView.findViewById(R.id.tvAleady);
            llSeat = itemView.findViewById(R.id.llSeat);
            llStatus = itemView.findViewById(R.id.llStatus);
        }

        public void bind(final Context context,
                         final ScriptRole model,
                         final OnItemClickListener listener) {
            ivJoin.setOnClickListener(view -> {
                listener.onJoin(getLayoutPosition());
            });
//            ivAudienceAvatar.setOnClickListener(view -> {
//                listener.onAvatar(getLayoutPosition());
//            });
            // 占位图片
            ImageLoader.loadImage(context, ivAudienceAvatar, model.avatar, R.mipmap.ic_test_bg);
            if (!TextUtils.isEmpty(model.name)) {
                tvAudienceName.setText(model.name);
            } else {
                tvAudienceName.setText("加载中");
            }
            if (!model.isUsed) {//玩家未进入座位
                ivBorder.setVisibility(View.GONE);
                ivJoin.setVisibility(View.VISIBLE);
                tvAlready.setVisibility(View.GONE);
                ivVoice.setVisibility(View.GONE);
//                ivAudienceAvatar.setImageResource(R.mipmap.ic_test_bg);
//                tvAudienceName.setText("");
//                mIvMute.setVisibility(View.GONE);
            } else {
//                ImageLoader.loadImage(context, ivAudienceAvatar, model.avatar, R.mipmap.ic_test_bg);
//                if (!TextUtils.isEmpty(model.name)) {
//                    tvAudienceName.setText(model.name);
//                } else {
//                    tvAudienceName.setText("加载中");
//                }
                ivBorder.setVisibility(View.VISIBLE);
                ivJoin.setVisibility(View.GONE);
                tvAlready.setVisibility(View.INVISIBLE);
                ivVoice.setVisibility(View.VISIBLE);
                boolean mute = model.isUserMute || model.isSeatMute;
//                ivVoice.setVisibility(mute ? View.VISIBLE : View.GONE);
                ivVoice.setImageResource(mute ? R.mipmap.ic_user_voice_off : R.mipmap.ic_user_voice_on);
                if (mute) {
                    ivBorder.setVisibility(View.VISIBLE);
                } else {
                    ivBorder.setVisibility(model.isTalk ? View.VISIBLE : View.GONE);
                }
            }

            //玩家准备状态
            if (!model.isReady) {
                tvAlready.setVisibility(View.GONE);
            } else {
                tvAlready.setVisibility(View.VISIBLE);
            }
        }
    }

    public static class DmViewHolder extends RecyclerView.ViewHolder {

        ImageView ivUserVoice, ivReady, ivBorder;
        QMUIRadiusImageView ivAudienceAvatar;
        TextView tvAudienceName;
        LinearLayout llSeat;

        public DmViewHolder(@NonNull View itemView) {
            super(itemView);
            ivUserVoice = itemView.findViewById(R.id.ivUserVoice);
            ivReady = itemView.findViewById(R.id.ivReady);
            ivBorder = itemView.findViewById(R.id.ivBorder);
            ivAudienceAvatar = itemView.findViewById(R.id.ivAudienceAvatar);
            tvAudienceName = itemView.findViewById(R.id.tvAudienceName);
            llSeat = itemView.findViewById(R.id.llSeat);
        }

        public void bind(final Context context,
                         final ScriptRole model,
                         final OnItemClickListener listener) {
            ivUserVoice.setOnClickListener(view -> {
                listener.onVoice(getLayoutPosition());
            });
//            ivAudienceAvatar.setOnClickListener(view -> {
//                listener.onAvatar(getLayoutPosition());
//            });
            // 占位图片
            ImageLoader.loadImage(context, ivAudienceAvatar, model.avatar, R.mipmap.ic_test_bg);
            if (!TextUtils.isEmpty(model.name)) {
                tvAudienceName.setText(model.name);
            } else {
                tvAudienceName.setText("加载中");
            }
            if (!model.isUsed) {//玩家未进入座位
                ivUserVoice.setVisibility(View.GONE);
                ivBorder.setVisibility(View.GONE);
//                ivAudienceAvatar.setImageResource(R.mipmap.ic_test_bg);
//                tvAudienceName.setText("");
//                mIvMute.setVisibility(View.GONE);
            } else {
                ivBorder.setVisibility(View.VISIBLE);
                ivUserVoice.setVisibility(View.VISIBLE);
                boolean mute = model.isUserMute || model.isSeatMute;
                ivUserVoice.setImageResource(mute ? R.mipmap.ic_user_voice_off : R.mipmap.ic_user_voice_on);
//                mIvMute.setVisibility(mute ? View.VISIBLE : View.GONE);
                if (mute) {
                    ivBorder.setVisibility(View.GONE);
                } else {
                    ivBorder.setVisibility(model.isTalk ? View.VISIBLE : View.GONE);
                }
            }

            //玩家准备状态
            if (!model.isReady) {
                ivReady.setVisibility(View.GONE);
            } else {
                ivReady.setVisibility(View.VISIBLE);
            }
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setOnPopupItemClickListener(OnPopupItemClickListener onPopupItemClickListener) {
        this.mOnPopupItemClickListener = onPopupItemClickListener;
    }

    public interface OnItemClickListener {

        void onJoin(int position);

        void onVoice(int position);
    }

    public interface OnPopupItemClickListener {
        void onUserPoster(int position);

        void onScriptPoster(int position);

        void onBgStory(int position);
    }
}
